package cn.itlight.basic.controller;

import cn.itlight.basic.annotation.PreAuthorize;
import cn.itlight.basic.exception.BusinessException;
import cn.itlight.basic.service.ILoginService;
import cn.itlight.basic.utils.AjaxResult;
import cn.itlight.basic.vo.LoginVo;
import cn.itlight.basic.vo.WxVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;
    /*
     * @Title: account
     * @Description:账户登录
     * @Author: TomLiu
     * @Version: 1.0
     * @Date:  2022/7/10 16:20
     * @Parameters:
     * @Return
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginVo loginVo){
        try {
            HashMap<String, Object> map = loginService.account(loginVo);
            return AjaxResult.me().setResultObj(map);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    /*
    * @Title: wechat
    * @Description: 微信登录
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/7/10 16:21
    * @Parameters:
    * @Return
    */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WxVo WxVo){
        try {
            return loginService.wechat(WxVo);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WxVo WxVo){
        try {
            Map<String, Object> map = loginService.binder(WxVo);
            return AjaxResult.me().setResultObj(map);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
}
